<?php
namespace app\index\controller;

use think\Controller;
use app\index\common;
use think\Db;
use think\Request;
use app\index\controller\Log\OperationLog as OL;

/**
 * Class: Network 全网商品类
 * Author: Jason ZHENG <mingjun.zheng@zengduokeji.com>
 * Date : 2018-08-09
 */
class Network extends Controller
{
	//商品同步功能api
	public function synchro(Request $request)
	{

		$postData = $request->post();
		$rules = [
            "id|商品id"			=> "require",
            "price|销售价"		=> "require",
            "synopsis|描述"		=> "require",
            "is_autodeliver|是否自发货" => "require|integer",
		];
		$user = $this->user;
		if($this->validate($postData, $rules) === true)
		{	
			$product = $this->validateProduct($postData['id'],$user,$postData);
			if($product['code'] != 666){
				return $product;
			}
			if( !empty($postData['delete_product_id']) ){
				if ( Db::table('product')->where(['id' => $postData['delete_product_id'] ])->update(['state' => 1]) ) {
					
				}else{
					return ReturnData::error(1001,'入网失败！ 请稍后重试！');
				}
			}
			$product = $product['data'];
			
			Db::startTrans();
			try{
				if($postData['is_autodeliver'] == 1){
					$diliveryInsert = [
						'user_id'		=> $user['id'],
						'province'		=> $postData['province'],
						'city'			=> $postData['city'],
						'district'		=> $postData['district'],
						'address'		=> $postData['address'],
						'phone'			=> $postData['phone'],
						'name'			=> $postData['name'], 
						'create_time'	=> date('Y-m-d H:i:s'),
						'update_time'	=> date('Y-m-d H:i:s'),
					];
					$deliveryId = Db::table('delivery')->insertGetId($diliveryInsert);
				}
				$productSynchroInsert =  [
					'system_id'		 => $product['system_id'],
					'business_id' 	 => $user['id'],
					'category_id'	 => $product['category_id'],
					'brand_id'		 => $product['brand_id'],
					'type_id'		 => $product['type_id'],
					'systematics_id' => $product['systematics_id'],
					'is_shelf'		 => $product['is_shelf'],
					'is_onsale'		 => 0,
					'pic_url'		 => $product['pic_url'],
					'thumb_url'		 => $product['thumb_url'],
					'product_sn'	 => $product['product_sn'],
					'product_name'	 => $product['product_name'],
					'full_name'		 => $product['full_name'],
					'procurement_status'	=> $product['procurement_status'],
					'createtine'	 => date('Y-m-d H:i:s'),
					'product_cate'	 => $product['product_cate'],
					'product_condition'	=> $product['product_condition'],
					'product_code'		=> $product['product_code'],
					'maintenance'		=> $product['maintenance'],
					'stock'				=> 1,
					'product_state'		=> 2,
					'is_synchro'		=> 1,
					'source_product_id'	=> $product['id'],
					'price'			 => $postData['price'],
					'peer_price'	 => $product['peer_price'],
					'synopsis'		 => $postData['synopsis'],
					'is_autodeliver' => $postData['is_autodeliver'],
					'delivery_id'	 => $postData['is_autodeliver'] == 1 ? $deliveryId : null,
				];
				$productSynchro = Db::table('product')->where(['business_id' => $user['id'], 'source_product_id'	 => $product['id'], 'state' => 0])->find();
				if(!empty($productSynchro)){
					Db::rollback();
					return ReturnData::error(1001,'您已经同步过该商品，请勿重新添加');
				}
				$productSynchroId = Db::table('product')->insertGetId($productSynchroInsert);
				//商品图片
				$productImages = Db::table('productpic')->where(['product_id'=>$product['id'] ])->select();

				if($productSynchroId){
					Db::table('product')->where(['id' => $product['id']])->update(['synchro_count' => $product['synchro_count'] + 1]);
					foreach ($productImages as $image) {
						Db::table('productpic')->insert(['product_id' => $productSynchroId, 'pic_url' =>$image['pic_url'], 'state' => 0, 'createtime' => date('Y-m-d H:i:s')]);
					}
					Db::commit();
					return ReturnData::ok(true,'同步成功');
				}else{
					Db::rollback();
					return ReturnData::error(1001,'网络异常，请稍后重试');
				}
			}catch(\Exception $e){
				Db::rollback();
				return ReturnData::error(1001,'同步失败，请稍后重试');
			}
		}else
		{
			return ReturnData::error(1001,$this->validate($postData, $rules));
		}
	}

	public function validateProduct($id, $user, $postData)
	{
		$product = Db::table('product')->where(['id' => $id,'state'=>0])->find();
		if(empty($product)){
			return ReturnData::error(1001,'该商品不存在');
		}elseif($product['is_shelf'] == 0){
			return ReturnData::error(1001,'该商品已下架');
		}elseif($product['is_network'] == 0){
			return ReturnData::error(1001,'该商品不在全网库存里');
		}elseif(!in_array($product['product_state'], [1,2,3,4])){
			return ReturnData::error(1001,'该商品不在可售状态');
		}elseif($product['business_id'] == $user['id']){
			return ReturnData::error(1001,'该商品是自家商品');
		}elseif($product['peer_price'] >= $postData['price']){
			return ReturnData::error(1001,'价格不能低于同行价');
		}elseif($postData['is_autodeliver'] == 1){
			if(!isset($postData['name']) || empty($postData['name'])){
				return ReturnData::error(1001,'请输入收货地址姓名');
			}elseif(!isset($postData['phone']) || empty($postData['phone'])){
				return ReturnData::error(1001,'请输入收货地址电话');
			}elseif(!isset($postData['province']) || empty($postData['province'])){
				return ReturnData::error(1001,'请输入收货地址省份');
			}elseif(!isset($postData['city']) || empty($postData['city'])){
				return ReturnData::error(1001,'请输入收货地址城市');
			}elseif(!isset($postData['district']) || empty($postData['district'])){
				return ReturnData::error(1001,'请输入收货地址地区');
			}elseif(!isset($postData['address']) || empty($postData['address'])){
				return ReturnData::error(1001,'请输入收货地址详细地址');
			}
		}
		return ReturnData::ok($product);
	}
}